package com.bicon.botu.mall.component.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bicon.botu.mall.component.entity.Dictionary;
import com.bicon.botu.mall.component.dto.DictionaryDTO;
import com.bicon.botu.mall.component.service.IComponentDictionaryService;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.BeanMapper;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 存放常用配置参数、地区、年龄段、性别、指标类型等数据 前端控制器
 * </p>
 *
 * @author tandk
 * @since 2017-07-31
 */
@RestController
@RequestMapping("/V1.0.0/base/dictionary")
public class DictionaryController extends BaseController {

    @Autowired
    IComponentDictionaryService componentDictionaryService;

    /**
     * 初始化所有字典缓存
     */
    @RequestMapping(value = "/initDictionaryRedis")
    public BaseResult initDictionaryRedis() {
        return componentDictionaryService.initDictionaryRedis() ? sendResult200() : sendResult500();
    }

    /**
     * 初始化所有字典缓存
     */
    @RequestMapping(value = "/initAdvantageAreaToRedis")
    public BaseResult initAdvantageAreaToRedis() {
        return componentDictionaryService.initAdvantageAreaToRedis() ? sendResult200() : sendResult500();
    }

    /**
     * 初始化基础字典缓存
     */
    @RequestMapping(value = "/initBaseDictionaryRedis")
    public BaseResult initBaseDictionaryRedis() {
        return componentDictionaryService.initBaseDictionaryRedis() ? sendResult200() : sendResult500();
    }

    /**
     * 初始化地区字典到redis
     */
    @RequestMapping(value = "/initAreaToRedis")
    public BaseResult initAreaToRedis() {
        return componentDictionaryService.initAreaToRedis() ? sendResult200() : sendResult500();
    }

    /**
     * 从redis中查询省、市、区
     *
     * @param code     查询（省、市、区）对应的code
     * @param parentId 父地区id
     */
    @RequestMapping(value = "/findAreaFromRedis")
    public BaseResult findAreaFromRedis(@RequestParam String code, @RequestParam int parentId) {
        return componentDictionaryService.findAreaFromRedis(code, parentId);
    }


    /**
     * 一次性返回省市区数据给web管理端
     * @return
     */
    @RequestMapping(value = "/findAllArea")
    public BaseResult findAllArea(){
        List<Dictionary> dictionaryList = new Dictionary().selectList(new EntityWrapper<Dictionary>().eq("type", 0));
        return  sendResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, dictionaryList);
    }

    /**
     * 新增字典
     *
     * @param name     字典名称
     * @param code     编码
     * @param parentId 父字典编号
     * @param type     类型
     * @param path     路径
     * @param level    等级
     * @param content  内容
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult add(@RequestParam String name, @RequestParam String code, @RequestParam Integer parentId, @RequestParam Integer type,
                          @RequestParam String path, @RequestParam Integer level, @RequestParam String content) {
        Dictionary dictionary = new Dictionary();
        dictionary.preInsert();
        dictionary.setDictionaryName(name);
        dictionary.setDictionaryCode(code);
        dictionary.setParentId(parentId);
        dictionary.setType(type);
        dictionary.setPath(path);
        dictionary.setLevel(level);
        dictionary.setContent(content);
        if (componentDictionaryService.addDictionary(dictionary)) {
            return sendResult200(dictionary);
        } else {
            return sendResult500();
        }
    }

    /**
     * 修改字典
     *
     * @param id       字典Id
     * @param name     字典名称
     * @param code     编码
     * @param parentId 父字典编号
     * @param type     类型
     * @param path     路径
     * @param level    等级
     * @param content  内容
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult update(@RequestParam Integer id, @RequestParam String name, @RequestParam String code, @RequestParam Integer parentId, @RequestParam Integer type,
                             @RequestParam String path, @RequestParam Integer level, @RequestParam String content) {
        Dictionary dictionary = new Dictionary();
        dictionary.preUpdate();
        dictionary.setId(id);
        dictionary.setDictionaryName(name);
        dictionary.setDictionaryCode(code);
        dictionary.setParentId(parentId);
        dictionary.setType(type);
        dictionary.setPath(path);
        dictionary.setLevel(level);
        dictionary.setContent(content);
        if (componentDictionaryService.updateDictionary(dictionary)) {
            return sendResult200(dictionary);
        }
        return sendResult500();
    }

    /**
     * 校验字典code是否存在
     *
     * @param code code
     */
    @RequestMapping(value = "/checkCodeIsExist")
    public BaseResult checkCodeIsExist(@RequestParam String code) {
        boolean b = true;
        int num = new Dictionary().selectCount(new EntityWrapper<Dictionary>().eq("dictionary_code", code).eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        if (num == 0) {
            b = false;
        }
        return sendResult200(b);
    }

    /**
     * 逻辑删除字典
     *
     * @param id 主键
     */
    @RequestMapping(value = "/del")
    public BaseResult del(@RequestParam int id) {
        if (componentDictionaryService.delDictionary(id)) {
            return sendResult200();
        }
        return sendResult500();
    }


    /**
     * 查询字典详情
     *
     * @param id 字典id
     */
    @RequestMapping(value = "/findDictionaryById")
    public BaseResult findDictionaryById(@RequestParam int id) {
        Map<String, Object> map = new HashedMap();
        Dictionary dictionary = new Dictionary().selectById(id);
        map.put("dictionary", dictionary);
        return sendResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, map);
    }


    /**
     * 根据父字典id从db获取字典集合
     *
     * @param parentId 父字典id
     */
    @RequestMapping(value = "/findListByParentId")
    public BaseResult findListByParentId(@RequestParam Integer parentId) {
        return sendResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, new Dictionary().selectList(new EntityWrapper<Dictionary>().eq("parent_id", parentId).eq("logic_delete", Constants.DEL_LOGIC_NORMAL)));
    }

    /**
     * 获取所有根字典
     */
    @RequestMapping(value = "/findRootDictionary")
    public BaseResult findRootDictionary() {
        Dictionary dictionary = new Dictionary();
        EntityWrapper<Dictionary> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("parent_id", 0);
        entityWrapper.eq("logic_delete", Constants.DEL_LOGIC_NORMAL);
        return sendResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, dictionary.selectList(entityWrapper));
    }

    /**
     * 根据父code获取子字典
     */
    @RequestMapping(value = "/findChildDictionary")
    public BaseResult findChildDictionary(@RequestParam String code) {
        Dictionary dictionary = new Dictionary().selectOne(new EntityWrapper<Dictionary>().eq("dictionary_code", code).eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        EntityWrapper<Dictionary> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("parent_id", dictionary.getId());
        entityWrapper.eq("logic_delete", Constants.DEL_LOGIC_NORMAL);
        List<Dictionary> list = dictionary.selectList(entityWrapper);
        List<DictionaryDTO> dtoList = BeanMapper.mapList(list, DictionaryDTO.class);
        return sendResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, dtoList);
    }

    /**
     * 根据字典id获取字典及子字典列表
     */
    @RequestMapping(value = "/findDictionaryListById")
    public BaseResult findDictionaryListByRootId(@RequestParam String id) {
        Dictionary dictionary = new Dictionary();
        EntityWrapper<Dictionary> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("logic_delete", Constants.DEL_LOGIC_NORMAL);
        entityWrapper.andNew("path like '" + id + ":%' or path=" + id);
        return sendResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, dictionary.selectList(entityWrapper));
    }
}
